<template>
  <view class="choose-community">
    <view class="current-location">
      <view class="title">
        当前地点
      </view>
      <view class="content">
        <text>{{currentLocation}}</text>
        <view @click="chooseLocation">
          <image src="../../static/icon/locate.png" />
          <text>重新定位</text>
        </view>
      </view>
    </view>

    <view class="community">
      <view class="title">
        附近社区
      </view>
      <view class="box-cell">
        <u-cell-group :border="false">
          <u-cell :border="false" v-for="item in communityList" :key="item.id" :name='item.title' :title="item.title"
            isLink @click="goChoose">
          </u-cell>
        </u-cell-group>
      </view>
    </view>
    <map @markertap='onmarkertap' name="" id="map" :markers='markers' :latitude='location.latitude'
      :longitude='location.longitude'></map>
  </view>
</template>

<script>
  import qqmapsdk from '../../utils/map.js'
  export default {
    data() {
      return {
        location: {
          latitude: 0,
          longitude: 0
        },
        markers: [{
          id: 1,
          latitude: 0,
          longitude: 0,
          title: '我的位置',
          width: 30,
          height: 30,
          iconPath: '/static/icon/mine-active.png'
        }],
        currentLocation: '定位中...',
        communityList: []
      };
    },
    onLoad() {
      this.initLocation(),
        this.searchCommunity()
    },
    methods: {
      initLocation() {
        uni.getLocation({
          type: 'gcj02',
          isHighAccuracy: true,
          success: (res) => {
            // console.log(res)
            this.updateLocation(res)
            this.initLocationText()
            this.searchCommunity()
          },
          fail(err) {
            console.log(err)
          }
        })
      },
      // 坐标解析
      initLocationText() {
        qqmapsdk.reverseGeocoder({
          location: this.location,
          success: (res, data) => {
            // console.log(res)
            this.currentLocation = res.result.address
          }

        })
      },
      // 重新获取位置
      chooseLocation() {
        uni.chooseLocation({
          ...this.location,
          success: (res) => {
            // console.log(res)
            if (!res.name) return
            this.currentLocation = res.address
            this.updateLocation(res)
            this.searchCommunity()

          }
        })
      },
      updateLocation(res) {
        this.location.latitude = res.latitude
        this.location.longitude = res.longitude
        this.markers[0].latitude = res.latitude
        this.markers[0].longitude = res.longitude
      },
      // 搜索附近小区
      searchCommunity() {
        qqmapsdk.search({
          location: this.location,
          keyword: '小区',
          page_size: 10,
          success: (res) => {
            // console.log(res)
            this.communityList = res.data,
              res.data.forEach(item => {
                this.markers.push({
                  id: item.id - 0,
                  longitude: item.location.lng,
                  latitude: item.location.lat,
                  width: 20,
                  height: 30,
                  title: item.title
                })
              })
          }
        })
      },
      // 点击小区
      goChoose(e) {
        // console.log(e)
        this.$store.commit('house/setPoint', e.name)
        uni.navigateTo({
          url: '/pkg-house/choose-house/choose-house'
        })
      },
      // 点击标记
      onmarkertap(e) {
        console.log(e.detail.markerId)
        const obj = this.communityList.find(item => {
          return item.id - 0 === e.detail.markerId
        })
        console.log(obj)

      }
    }
  }
</script>

<style lang="scss">
  #map {
    width: 750rpx;
    height: 750rpx;
  }

  .choose-community {
    height: 100%;
    background-color: #FAFAFA;

    .current-location {
      .title {
        padding: 0 30rpx;
        line-height: 86rpx;
      }

      .content {
        width: 750rpx;
        height: 102rpx;
        background-color: #fff;
        box-sizing: border-box;
        padding: 0 30rpx;
        display: flex;
        justify-content: space-between;
        align-items: center;

        text {
          font-size: 28rpx;
          color: #333;
        }

        view {
          display: flex;
          align-items: center;

          image {
            width: 30rpx;
            height: 30rpx;
            margin-right: 6rpx;
          }

          text {
            color: #5591AF;
          }
        }
      }
    }

    .community {
      .title {
        padding: 0 30rpx;
        line-height: 86rpx;
      }

      .box-cell {
        background: #ffffff;
      }

      ::v-deep .u-cell__body {
        background-color: #fff;
        height: 102rpx;
        // padding: 0;
        // margin: 0 15px;
        border-bottom: 1px solid #FAFAFA;

      }
    }
  }
</style>
